package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.CarConstans;
import com.ruoyi.system.domain.SysAlarms;
import com.ruoyi.system.domain.SysCars;
import com.ruoyi.system.domain.SysOwnerCars;
import com.ruoyi.system.service.ISysAlarmsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysRepairInfo;
import com.ruoyi.system.service.ISysRepairInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 维修信息表Controller
 * 
 * @author ruoyi
 * @date 2022-05-11
 */
@RestController
@RequestMapping("/system/sysRepairInfo")
public class SysRepairInfoController extends BaseController
{
    @Autowired
    private ISysRepairInfoService sysRepairInfoService;

    @Autowired
    private ISysAlarmsService sysAlarmsService;

    /**
     * 查询维修信息表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysRepairInfo sysRepairInfo)
    {
        if (!"admin".equals(getUsername())) {
            Long userId = getUserId();
            sysRepairInfo.setMaintenanceId(userId);
        }
        startPage();
        List<SysRepairInfo> list = sysRepairInfoService.selectSysRepairInfoList(sysRepairInfo);
        return getDataTable(list);
    }

    /**
     * 导出维修信息表列表
     */
    @Log(title = "维修信息表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRepairInfo sysRepairInfo)
    {
        List<SysRepairInfo> list = sysRepairInfoService.selectSysRepairInfoList(sysRepairInfo);
        ExcelUtil<SysRepairInfo> util = new ExcelUtil<SysRepairInfo>(SysRepairInfo.class);
        util.exportExcel(response, list, "维修信息表数据");
    }

    /**
     * 获取维修信息表详细信息
     */
    @GetMapping(value = "/{repairId}")
    public AjaxResult getInfo(@PathVariable("repairId") Long repairId)
    {
        return AjaxResult.success(sysRepairInfoService.selectSysRepairInfoByRepairId(repairId));
    }

    /**
     * 新增维修信息表
     */
    @Log(title = "维修信息表", businessType = BusinessType.INSERT)
    @PostMapping("/{alarmId}")
    public AjaxResult add(@RequestBody SysRepairInfo sysRepairInfo, @PathVariable("alarmId") Long alarmId)
    {
        if (sysRepairInfo == null) return error("空数据!");
        return toAjax(sysRepairInfoService.insertSysRepairInfo(sysRepairInfo, alarmId));
    }

    /**
     * 修改维修信息表
     */
    @Log(title = "维修信息表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRepairInfo sysRepairInfo)
    {
        if(sysRepairInfo == null) return error("空数据！");
        Long isAccept = sysRepairInfo.getIsAccept();
        Long state = sysRepairInfo.getState();

        //更新故障信息
        SysAlarms sysAlarm = new SysAlarms();
        //resId和故障记录相关联，所以唯一确定，获取故障信息
        sysAlarm.setResId(sysRepairInfo.getResId());
        List<SysAlarms> sysAlarms = sysAlarmsService.selectSysAlarmsList(sysAlarm);
        if (sysAlarms == null || sysAlarms.size() == 0) return error("管理员已删除该故障记录！");
        sysAlarm = sysAlarms.get(0);

        //接收
        if (CarConstans.STATE_ACCEPTED.equals(isAccept) && CarConstans.STATE_UNREPAIRED.equals(state)) {
            //更新故障信息
            sysAlarm.setState(CarConstans.STATE_ASSIGN_ACCEPTED);
            sysAlarmsService.updateSysAlarms(sysAlarm);
            //更新修理状态
            sysRepairInfo.setState(CarConstans.STATE_REPAIRING);
        }
        //拒绝接收
        else if (CarConstans.STATE_REFUSED.equals(isAccept) && CarConstans.STATE_UNREPAIRED.equals(state)) {
            //更新故障信息
            sysAlarm.setState(CarConstans.STATE_REFUSED);
            sysAlarmsService.updateSysAlarms(sysAlarm);
        }
        //完成
        else if (CarConstans.STATE_REPAIRED.equals(state)) {
            sysAlarm.setState(CarConstans.STATE_COMPLETED);
            sysAlarmsService.updateSysAlarms(sysAlarm);
            //更新修理状态
            sysRepairInfo.setState(CarConstans.STATE_REPAIRED);
        }
        return toAjax(sysRepairInfoService.updateSysRepairInfo(sysRepairInfo));
    }

    /**
     * 删除维修信息表
     */
    @Log(title = "维修信息表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{repairIds}")
    public AjaxResult remove(@PathVariable Long[] repairIds)
    {
        return toAjax(sysRepairInfoService.deleteSysRepairInfoByRepairIds(repairIds));
    }
}
